<html>
<head><meta charset="utf-8"><title>Await multiple futures · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html">Await multiple futures</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="191968451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191968451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191968451">(Mar 27 2020 at 01:17)</a>:</h4>
<p>Hello! Not sure if this is the place to ask this, but I'm trying to build a vec of futures and await/join them in a non-async function. Something like this:</p>
<div class="codehilite"><pre><span></span>fn some_function() {
                    use futures::executor::block_on;
                    use futures::future::join_all;
                    use futures::task::SpawnExt;

                    let futs: Vec&lt;_&gt; = some_vec
                        .into_iter()
                        .map(|cl| tokio::task::spawn_blocking(async || { some_async_function(cl).await }))
                        .collect();

                    let res: Vec&lt;CodeLens&gt; = block_on(join_all(c))
                        .into_iter()
                        .map(|c| c.unwrap())
                        .collect();
}
</pre></div>


<p>My intention is to basically spawn that vec of futures and await them in parallel, to then join them to the main thread, but I'm now getting this error, on the last collect:</p>
<div class="codehilite"><pre><span></span>the trait std::iter::FromIterator&lt;impl core::future::future::Future&gt; is not implemented for std::vec::Vec&lt;lsp_types::CodeLens&gt;
</pre></div>


<p>Can someone help me understand what I'm doing wrong here? (or a better way to do this)</p>



<a name="191984142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191984142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191984142">(Mar 27 2020 at 07:37)</a>:</h4>
<p>I think you wanted to say <code>block_on(join_all(futs))</code> instead of <code>block_on(join_all(c))</code>? But it does seem written in a round-about way, e.g. I don't think you need <code>spawn_blocking</code> since your future doesn't block so <code>spawn</code> might be enough. There's also <code>FuturesUnordered</code> which may be more efficient than <code>join_all</code>.</p>



<a name="191984297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191984297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191984297">(Mar 27 2020 at 07:39)</a>:</h4>
<p>Assuming that <code>c</code> is a typo, I'm still not sure why you're getting the error, since <code>block_on(join_all(futs))</code> should return a <code>Vec&lt;Option&lt;CodeLens&gt;&gt;</code> if  <code>some_async_function</code> returns an <code>Option&lt;CodeLens&gt;</code>.</p>



<a name="191990607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191990607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191990607">(Mar 27 2020 at 09:04)</a>:</h4>
<p>Ah yeah that was a typo, my bad. <code>some_async_function</code> there returns a <code>failure::Fallible&lt;CodeLens&gt;</code>.</p>



<a name="191991443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191991443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191991443">(Mar 27 2020 at 09:14)</a>:</h4>
<p>So you're getting that error on the <code>block_on</code> line?</p>



<a name="191991567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191991567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191991567">(Mar 27 2020 at 09:16)</a>:</h4>
<p>Try checking the types, or comment out bits of code to see where is the type mismatch. From that error I get that <code>block_on(join_all(c)).into_iter().map(|c| c.unwrap())</code> yields <code>Future</code>s</p>



<a name="191991645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191991645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191991645">(Mar 27 2020 at 09:17)</a>:</h4>
<p>ok I managed to get it working, had to change that to a <code>spawn</code> and was missing a second <code>unwrap</code> on that map in the end. I didn't know spawn returned a result though <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="191992458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Await%20multiple%20futures/near/191992458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Await.20multiple.20futures.html#191992458">(Mar 27 2020 at 09:27)</a>:</h4>
<p>also, thank you! <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>